package com.base.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.system.exception.HttpException;
import com.base.system.jpa.entity.JpaPermission;
import com.base.system.jpa.repository.PermissionRepository;
import com.base.system.mybatisPlus.entity.MybatisPermission;
import com.base.system.mybatisPlus.mapper.PermissionMapper;
import com.base.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private PermissionRepository permissionRepository;

    @Override
    public List<JpaPermission> SaveBatch(List<JpaPermission> permissions) {
        Map<String, JpaPermission> collect2 = permissions.stream().collect(Collectors.toMap(t -> t.getPerkey(), a -> a, (a1, a2) -> a2));
        permissions = collect2.values().stream().collect(Collectors.toList());
        List<MybatisPermission> mybatisPermissions = permissionMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isEmpty(mybatisPermissions)) return permissionRepository.saveAll(permissions);
        else {
            Set<String> collect = mybatisPermissions.stream().map(t -> t.getPerkey()).collect(Collectors.toSet());
            List<JpaPermission> collect1 = permissions.stream().filter(t -> !collect.contains(t.getPerkey())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(collect1)) return permissionRepository.saveAll(collect1);
            return null;
        }
    }

    @Override
    public IPage<MybatisPermission> page(Page page, MybatisPermission permission) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StrUtil.isNotEmpty(permission.getNameZh()), "name_zh", permission.getNameZh());
        queryWrapper.like(StrUtil.isNotEmpty(permission.getPerkey()), "per_key", permission.getPerkey());
        queryWrapper.orderByDesc("create_time");
        return permissionMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void save(MybatisPermission permission) {
        valid(permission, true);
        permissionMapper.insert(permission);
    }

    private void valid(MybatisPermission permission, boolean isAdd) {
        Optional<JpaPermission> firstByNameZh = permissionRepository.findFirstByNameZh(permission.getNameZh());
        if (firstByNameZh.isPresent() && (!firstByNameZh.get().getId().equals(permission.getId()) || isAdd))
            throw new HttpException("角色名称重复");
        Optional<JpaPermission> firstByPerkey = permissionRepository.findFirstByPerkey(permission.getPerkey());
        if (firstByPerkey.isPresent() && (!firstByPerkey.get().getId().equals(permission.getId()) || isAdd))
            throw new HttpException("角色key值重复");
    }

    @Override
    public void update(MybatisPermission permission) {
        valid(permission, false);
        permissionMapper.updateById(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteById(id);
    }

    @Override
    public List<JpaPermission> all() {
        return permissionRepository.findAll();
    }
}
